﻿using EventBus.Abstractions;
using EventBus.Simple;
using GraphQL;
using GraphQL.Server;
using GraphQL.Server.Ui.Playground;
using GraphQLSample.Application.DomainEventHandlers;
using GraphQLSample.Domain.Schema;
using GraphQLSample.Domain.Services;
using GraphQLSample.Infrastructure.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace GraphQLSample
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationContext>(opt =>
            {
                var connString = Configuration.GetConnectionString("Default");
                opt.UseNpgsql(connString);
            }, ServiceLifetime.Singleton);
            services.AddScoped<UserType>();
            services.AddScoped<RoleType>();
            services.AddScoped<SexEnumType>();
            services.AddScoped<UserQuery>();
            services.AddScoped<UserInputType>();
            services.AddScoped<UserMutation>();
            services.AddScoped<UserSchema>();
            services.AddScoped<IDependencyResolver>(sp => new FuncDependencyResolver(sp.GetRequiredService));

            services.AddScoped<IUserService, UserService>();
            services.AddScoped<IRoleService, RoleService>();
            services.AddGraphQL(opt =>
            {
                opt.EnableMetrics = true;
                opt.ExposeExceptions = false;
            })
            .AddWebSockets()
            .AddDataLoader();

            services.AddSingleton<IEventHandler, UserCreatedEventHandler>();
            services.AddSingleton<IEventHandlerExecutionContext>(sp => new EventHandlerExecutionContext(services, s => s.BuildServiceProvider()));
            services.AddSingleton<IEventBus, PassThroughEventBus>();
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,IServiceProvider sp)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
            eventBus.Subscribe<UserCreatedEvent,UserCreatedEventHandler>();
            app.UseMvc();
            app.UseWebSockets();
            app.UseGraphQL<UserSchema>("/graphql");
            app.UseGraphQLWebSockets<UserSchema>("/graphql");
            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}
